//
//  VisitorLoginView.swift
//  Weibo
//
//  Created by 周先生 on 15/11/8.
//  Copyright © 2015年 周先生. All rights reserved.
//

import UIKit

//定义协议
protocol VisitorLoginViewDelegate: NSObjectProtocol {

    //协议方法
    //登陆
    func visitorWillLogin()
    //注册
    func visitorWillRegister()

}
class VisitorLoginView: UIView {
    
    //声明代理属性
    //属性默认是强引用 需要使用weak
    weak var visitorDelegate:VisitorLoginViewDelegate?
    
    //代理调用协议方法
   @objc func loginDidBtnClick() {
    visitorDelegate?.visitorWillLogin()
    }
   @objc func registerDidBtnClick() {
    visitorDelegate?.visitorWillRegister()
    }
    //重写构造方法
    init() {
        super.init(frame: CGRectZero)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置页面信息
    func setUIInfo(imageName: String?, title: String) {
        iconView.hidden = false
        tipLabel.text = title
        if imageName != nil {
            circleView.image = UIImage(named: imageName!)
            bringSubviewToFront(circleView)//取消对其它页面的覆盖
            iconView.hidden = true //隐藏iconView大头标
        }else {
            //转起来
            startAnimation()
        }
    }
    //转动图片背景
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = 0
        anim.toValue = 2 * M_PI
        anim.duration = 20;
        //当动画结束时或者视图处于非活动状态 动画不移除图层
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    
    }
    //设置访客视图
    private func setupUI() {
//        backgroundColor = UIColor.redColor()
        addSubview(circleView)
        addSubview(iconView)
        addSubview(backView)
        addSubview(tipLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        //设置frame布局失效
        for v in subviews {
        v.translatesAutoresizingMaskIntoConstraints = false
        }
            //设置布局 VFL
            //"view1.attr1 = view2.attr2 * multiplier + constant"

            //布局的约束尽量相对一个控件
            //圆圈的约束
           addConstraint( NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
            addConstraint( NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
            //大头标的约束
            addConstraint( NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
            addConstraint( NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
            //文案的约束
            addConstraint( NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
            addConstraint( NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 26))
            //宽度约束
            addConstraint( NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
            addConstraint( NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
            //设置登陆按钮的约束
            addConstraint( NSLayoutConstraint(item: loginButton, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
            addConstraint( NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
            //宽高
            addConstraint( NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
            addConstraint( NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
            //设置注册按钮的约束
            addConstraint( NSLayoutConstraint(item: registerButton, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
            addConstraint( NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
            //宽高
            addConstraint( NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
            addConstraint( NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
            
            //设置背景图片的约束
            //位移枚举 || []
            //对试图宽高约束的数值 [String: NSNumber]
            addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView": backView]))
            addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-45)-[registerButton]", options: [], metrics: nil, views: ["backView": backView, "registerButton": registerButton]))

            //设置背景颜色
            backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加登陆 注册 按钮的点击事件
        loginButton.addTarget(self, action: "loginDidBtnClick", forControlEvents: .TouchUpInside)
        registerButton.addTarget(self, action: "registerDidBtnClick", forControlEvents: .TouchUpInside)
        
    }

    
    //懒加载所有的控件
        private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //提示文案
    private lazy var tipLabel: UILabel = {
    
        let l = UILabel()
        l.text = "关注一些人,回这里看看有什么惊喜关注一些人,回这里看看有什么惊喜"
        l.textAlignment = NSTextAlignment.Center
        l.font = UIFont.systemFontOfSize(14)
        l.textColor = UIColor.blueColor()
        l.numberOfLines = 0
        return l
    }()
    //登陆按钮
    private lazy var loginButton: UIButton = {
        let loginBtn = UIButton()
        loginBtn.setTitle("登陆", forState: .Normal)
        loginBtn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return loginBtn
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        let registerBtn = UIButton()
        registerBtn.setTitle("注册", forState: .Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return registerBtn
    }()

}







